const path = require('path');
const rspack = require('@rspack/core');

const rimraf = require('rimraf');

function resolve(...src) {
  return path.resolve(__dirname, ...src);
}


console.log('环境', process.env.NODE_ENV);
if (process.env.NODE_ENV === 'production') {
  rimraf('dist', {}, () => console.log('Removed all previous build assets.'));
}

module.exports = async () => ({
  mode: 'development',
  entry: {
    yapi: './src/views/Yapi/ui/index.tsx',
  },
  output: {
    filename: '[name].js',
    path: resolve('dist/ui'),
    publicPath: '/',
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js', '.jsx' ],
    alias: {
      '@': resolve('src'),
    },
  },
  module: {
    rules: [
      {
        test: /\.(tsx|ts|js|jsx)?$/,
        use: {
          loader: 'builtin:swc-loader',
          options: {
            tsc: {
              parser: {
                syntax: 'ecmascript',
                jsx: true,
              },
              transform: {
                react: {
                  pragma: 'React.createElement',
                  pragmaFrag: 'React.Fragment',
                  throwIfNamespace: true,
                  development: false,
                  useBuiltins: false,
                },
              },
            },
          },
        },
        type: 'javascript/auto',
      },
      {
        test: /\.css$/,
        use: [
          'style-loader', // ✅ 注入样式到 JS
          'css-loader', // ✅ 解析 CSS 文件
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                // ...
              },
            },
          },
        ],
      },
    ],
  },
  ...(process.env.NODE_ENV !== 'production' ? { devtool: 'inline-source-map' } : {}),
  plugins: [
    new rspack.CssExtractRspackPlugin({}),
  ],
  optimization: {
    realContentHash: true,
  },
  experiments: {
    css: false,
  },
});
